
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>queue --- 一个同步的队列类 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="_thread --- 底层多线程 API" href="_thread.html" />
    <link rel="prev" title="sched --- 事件调度器" href="sched.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/queue.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="_thread.html" title="_thread --- 底层多线程 API"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="sched.html" title="sched --- 事件调度器"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="concurrency.html" accesskey="U">并发执行</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-queue">
<span id="queue-a-synchronized-queue-class"></span><h1><a class="reference internal" href="#module-queue" title="queue: A synchronized queue class."><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code></a> --- 一个同步的队列类<a class="headerlink" href="#module-queue" title="永久链接至标题">¶</a></h1>
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/queue.py">Lib/queue.py</a></p>
<hr class="docutils" />
<p><a class="reference internal" href="#module-queue" title="queue: A synchronized queue class."><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code></a> 模块实现多生产者，多消费者队列。当信息必须安全的在多线程之间交换时，它在线程编程中是特别有用的。此模块中的 <a class="reference internal" href="#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a> 类实现了所有锁定需求的语义。它依赖于Python支持的线程可用性；请参阅 <a class="reference internal" href="threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> 模块。</p>
<p>模块实现了三种类型的队列，它们的区别仅仅是条目取回的顺序。在 <abbr title="first-in, first-out">FIFO</abbr> 队列中，先添加的任务先取回。在 <abbr title="last-in, first-out">LIFO</abbr> 队列中，最近被添加的条目先取回(操作类似一个堆栈)。优先级队列中，条目将保持排序( 使用 <a class="reference internal" href="heapq.html#module-heapq" title="heapq: Heap queue algorithm (a.k.a. priority queue)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">heapq</span></code></a> 模块 ) 并且最小值的条目第一个返回。</p>
<p>在内部，这三个类型的队列使用锁来临时阻塞竞争线程；然而，它们并未被设计用于线程的重入性处理。</p>
<p>此外，模块实现了一个 &quot;简单的&quot;  <abbr title="first-in, first-out">FIFO</abbr> 队列类型， <a class="reference internal" href="#queue.SimpleQueue" title="queue.SimpleQueue"><code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleQueue</span></code></a> ，这个特殊实现为小功能在交换中提供额外的保障。</p>
<p><a class="reference internal" href="#module-queue" title="queue: A synchronized queue class."><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code></a> 模块定义了下列类和异常：</p>
<dl class="class">
<dt id="queue.Queue">
<em class="property">class </em><code class="sig-prename descclassname">queue.</code><code class="sig-name descname">Queue</code><span class="sig-paren">(</span><em class="sig-param">maxsize=0</em><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue" title="永久链接至目标">¶</a></dt>
<dd><p>Constructor for a <abbr title="first-in, first-out">FIFO</abbr> queue.  <em>maxsize</em> is
an integer that sets the upperbound
limit on the number of items that can be placed in the queue.  Insertion will
block once this size has been reached, until queue items are consumed.  If
<em>maxsize</em> is less than or equal to zero, the queue size is infinite.</p>
</dd></dl>

<dl class="class">
<dt id="queue.LifoQueue">
<em class="property">class </em><code class="sig-prename descclassname">queue.</code><code class="sig-name descname">LifoQueue</code><span class="sig-paren">(</span><em class="sig-param">maxsize=0</em><span class="sig-paren">)</span><a class="headerlink" href="#queue.LifoQueue" title="永久链接至目标">¶</a></dt>
<dd><p><abbr title="last-in, first-out">LIFO</abbr> 队列构造函数。 <em>maxsize</em> 是个整数，用于设置可以放入队列中的项目数的上限。当达到这个大小的时候，插入操作将阻塞至队列中的项目被消费掉。如果 <em>maxsize</em> 小于等于零，队列尺寸为无限大。</p>
</dd></dl>

<dl class="class">
<dt id="queue.PriorityQueue">
<em class="property">class </em><code class="sig-prename descclassname">queue.</code><code class="sig-name descname">PriorityQueue</code><span class="sig-paren">(</span><em class="sig-param">maxsize=0</em><span class="sig-paren">)</span><a class="headerlink" href="#queue.PriorityQueue" title="永久链接至目标">¶</a></dt>
<dd><p>优先级队列构造函数。 <em>maxsize</em> 是个整数，用于设置可以放入队列中的项目数的上限。当达到这个大小的时候，插入操作将阻塞至队列中的项目被消费掉。如果 <em>maxsize</em> 小于等于零，队列尺寸为无限大。</p>
<p>最小值先被取出( 最小值条目是由 <code class="docutils literal notranslate"><span class="pre">sorted(list(entries))[0]</span></code> 返回的条目)。条目的典型模式是一个以下形式的元组： <code class="docutils literal notranslate"><span class="pre">(priority_number,</span> <span class="pre">data)</span></code> 。</p>
<p>如果 <em>data</em> 元素没有可比性，数据将被包装在一个类中，忽略数据值，仅仅比较优先级数字 ：</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">dataclasses</span> <span class="kn">import</span> <span class="n">dataclass</span><span class="p">,</span> <span class="n">field</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Any</span>

<span class="nd">@dataclass</span><span class="p">(</span><span class="n">order</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">PrioritizedItem</span><span class="p">:</span>
    <span class="n">priority</span><span class="p">:</span> <span class="nb">int</span>
    <span class="n">item</span><span class="p">:</span> <span class="n">Any</span><span class="o">=</span><span class="n">field</span><span class="p">(</span><span class="n">compare</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="queue.SimpleQueue">
<em class="property">class </em><code class="sig-prename descclassname">queue.</code><code class="sig-name descname">SimpleQueue</code><a class="headerlink" href="#queue.SimpleQueue" title="永久链接至目标">¶</a></dt>
<dd><p>无界的 <abbr title="first-in, first-out">FIFO</abbr> 队列构造函数。简单的队列，缺少任务跟踪等高级功能。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能.</span></p>
</div>
</dd></dl>

<dl class="exception">
<dt id="queue.Empty">
<em class="property">exception </em><code class="sig-prename descclassname">queue.</code><code class="sig-name descname">Empty</code><a class="headerlink" href="#queue.Empty" title="永久链接至目标">¶</a></dt>
<dd><p>对空的 <a class="reference internal" href="#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a> 对象，调用非阻塞的 <a class="reference internal" href="#queue.Queue.get" title="queue.Queue.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code></a> (or  <a class="reference internal" href="#queue.Queue.get_nowait" title="queue.Queue.get_nowait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_nowait()</span></code></a>) 时，引发的异常。</p>
</dd></dl>

<dl class="exception">
<dt id="queue.Full">
<em class="property">exception </em><code class="sig-prename descclassname">queue.</code><code class="sig-name descname">Full</code><a class="headerlink" href="#queue.Full" title="永久链接至目标">¶</a></dt>
<dd><p>对满的 <a class="reference internal" href="#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a> 对象，调用非阻塞的 <a class="reference internal" href="#queue.Queue.put" title="queue.Queue.put"><code class="xref py py-meth docutils literal notranslate"><span class="pre">put()</span></code></a> (or <a class="reference internal" href="#queue.Queue.put_nowait" title="queue.Queue.put_nowait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">put_nowait()</span></code></a>) 时，引发的异常。</p>
</dd></dl>

<div class="section" id="queue-objects">
<span id="queueobjects"></span><h2>Queue对象<a class="headerlink" href="#queue-objects" title="永久链接至标题">¶</a></h2>
<p>队列对象 (<a class="reference internal" href="#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a>, <a class="reference internal" href="#queue.LifoQueue" title="queue.LifoQueue"><code class="xref py py-class docutils literal notranslate"><span class="pre">LifoQueue</span></code></a>, 或者 <a class="reference internal" href="#queue.PriorityQueue" title="queue.PriorityQueue"><code class="xref py py-class docutils literal notranslate"><span class="pre">PriorityQueue</span></code></a>) 提供下列描述的公共方法。</p>
<dl class="method">
<dt id="queue.Queue.qsize">
<code class="sig-prename descclassname">Queue.</code><code class="sig-name descname">qsize</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue.qsize" title="永久链接至目标">¶</a></dt>
<dd><p>返回队列的大致大小。注意，qsize() &gt; 0 不保证后续的 get() 不被阻塞，qsize() &lt; maxsize 也不保证 put() 不被阻塞。</p>
</dd></dl>

<dl class="method">
<dt id="queue.Queue.empty">
<code class="sig-prename descclassname">Queue.</code><code class="sig-name descname">empty</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue.empty" title="永久链接至目标">¶</a></dt>
<dd><p>如果队列为空，返回 <code class="docutils literal notranslate"><span class="pre">True</span></code> ，否则返回 <code class="docutils literal notranslate"><span class="pre">False</span></code> 。如果 empty() 返回 <code class="docutils literal notranslate"><span class="pre">True</span></code> ，不保证后续调用的 put() 不被阻塞。类似的，如果 empty() 返回 <code class="docutils literal notranslate"><span class="pre">False</span></code> ，也不保证后续调用的 get() 不被阻塞。</p>
</dd></dl>

<dl class="method">
<dt id="queue.Queue.full">
<code class="sig-prename descclassname">Queue.</code><code class="sig-name descname">full</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue.full" title="永久链接至目标">¶</a></dt>
<dd><p>如果队列是满的返回 <code class="docutils literal notranslate"><span class="pre">True</span></code> ，否则返回 <code class="docutils literal notranslate"><span class="pre">False</span></code> 。如果 full() 返回 <code class="docutils literal notranslate"><span class="pre">True</span></code> 不保证后续调用的 get() 不被阻塞。类似的，如果 full() 返回 <code class="docutils literal notranslate"><span class="pre">False</span></code> 也不保证后续调用的 put() 不被阻塞。</p>
</dd></dl>

<dl class="method">
<dt id="queue.Queue.put">
<code class="sig-prename descclassname">Queue.</code><code class="sig-name descname">put</code><span class="sig-paren">(</span><em class="sig-param">item</em>, <em class="sig-param">block=True</em>, <em class="sig-param">timeout=None</em><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue.put" title="永久链接至目标">¶</a></dt>
<dd><p>将 <em>item</em> 放入队列。如果可选参数 <em>block</em> 是 true 并且 <em>timeout</em> 是 <code class="docutils literal notranslate"><span class="pre">None</span></code> (默认)，则在必要时阻塞至有空闲插槽可用。如果 <em>timeout</em> 是个正数，将最多阻塞 <em>timeout</em> 秒，如果在这段时间没有可用的空闲插槽，将引发 <a class="reference internal" href="#queue.Full" title="queue.Full"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Full</span></code></a> 异常。反之 (<em>block</em> 是 false)，如果空闲插槽立即可用，则把 <em>item</em> 放入队列，否则引发 <a class="reference internal" href="#queue.Full" title="queue.Full"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Full</span></code></a> 异常 ( 在这种情况下，<em>timeout</em> 将被忽略)。</p>
</dd></dl>

<dl class="method">
<dt id="queue.Queue.put_nowait">
<code class="sig-prename descclassname">Queue.</code><code class="sig-name descname">put_nowait</code><span class="sig-paren">(</span><em class="sig-param">item</em><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue.put_nowait" title="永久链接至目标">¶</a></dt>
<dd><p>相当于 <code class="docutils literal notranslate"><span class="pre">put(item,</span> <span class="pre">False)</span></code> 。</p>
</dd></dl>

<dl class="method">
<dt id="queue.Queue.get">
<code class="sig-prename descclassname">Queue.</code><code class="sig-name descname">get</code><span class="sig-paren">(</span><em class="sig-param">block=True</em>, <em class="sig-param">timeout=None</em><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue.get" title="永久链接至目标">¶</a></dt>
<dd><p>从队列中移除并返回一个项目。如果可选参数 <em>block</em> 是 true 并且 <em>timeout</em> 是 <code class="docutils literal notranslate"><span class="pre">None</span></code> (默认值)，则在必要时阻塞至项目可得到。如果 <em>timeout</em> 是个正数，将最多阻塞 <em>timeout</em> 秒，如果在这段时间内项目不能得到，将引发 <a class="reference internal" href="#queue.Empty" title="queue.Empty"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Empty</span></code></a> 异常。反之 (<em>block</em> 是 false) , 如果一个项目立即可得到，则返回一个项目，否则引发 <a class="reference internal" href="#queue.Empty" title="queue.Empty"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Empty</span></code></a> 异常 (这种情况下，<em>timeout</em> 将被忽略)。</p>
<p>POSIX系统3.0之前，以及所有版本的Windows系统中，如果 <em>block</em> 是 true 并且 <em>timeout</em> 是 <code class="docutils literal notranslate"><span class="pre">None</span></code> ， 这个操作将进入基础锁的不间断等待。这意味着，没有异常能发生，尤其是 SIGINT 将不会触发 <a class="reference internal" href="exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a> 异常。</p>
</dd></dl>

<dl class="method">
<dt id="queue.Queue.get_nowait">
<code class="sig-prename descclassname">Queue.</code><code class="sig-name descname">get_nowait</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue.get_nowait" title="永久链接至目标">¶</a></dt>
<dd><p>相当于 <code class="docutils literal notranslate"><span class="pre">get(False)</span></code> 。</p>
</dd></dl>

<p>提供了两个方法，用于支持跟踪 排队的任务 是否 被守护的消费者线程 完整的处理。</p>
<dl class="method">
<dt id="queue.Queue.task_done">
<code class="sig-prename descclassname">Queue.</code><code class="sig-name descname">task_done</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue.task_done" title="永久链接至目标">¶</a></dt>
<dd><p>表示前面排队的任务已经被完成。被队列的消费者线程使用。每个 <a class="reference internal" href="#queue.Queue.get" title="queue.Queue.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code></a> 被用于获取一个任务， 后续调用 <a class="reference internal" href="#queue.Queue.task_done" title="queue.Queue.task_done"><code class="xref py py-meth docutils literal notranslate"><span class="pre">task_done()</span></code></a> 告诉队列，该任务的处理已经完成。</p>
<p>如果 <a class="reference internal" href="#queue.Queue.join" title="queue.Queue.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> 当前正在阻塞，在所有条目都被处理后，将解除阻塞(意味着每个 <a class="reference internal" href="#queue.Queue.put" title="queue.Queue.put"><code class="xref py py-meth docutils literal notranslate"><span class="pre">put()</span></code></a> 进队列的条目的 <a class="reference internal" href="#queue.Queue.task_done" title="queue.Queue.task_done"><code class="xref py py-meth docutils literal notranslate"><span class="pre">task_done()</span></code></a> 都被收到)。</p>
<p>如果被调用的次数多于放入队列中的项目数量，将引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> 异常 。</p>
</dd></dl>

<dl class="method">
<dt id="queue.Queue.join">
<code class="sig-prename descclassname">Queue.</code><code class="sig-name descname">join</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue.join" title="永久链接至目标">¶</a></dt>
<dd><p>阻塞至队列中所有的元素都被接收和处理完毕。</p>
<p>当条目添加到队列的时候，未完成任务的计数就会增加。每当消费者线程调用 <a class="reference internal" href="#queue.Queue.task_done" title="queue.Queue.task_done"><code class="xref py py-meth docutils literal notranslate"><span class="pre">task_done()</span></code></a> 表示这个条目已经被回收，该条目所有工作已经完成，未完成计数就会减少。当未完成计数降到零的时候， <a class="reference internal" href="#queue.Queue.join" title="queue.Queue.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> 阻塞被解除。</p>
</dd></dl>

<p>如何等待排队的任务被完成的示例：</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">worker</span><span class="p">():</span>
    <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
        <span class="n">item</span> <span class="o">=</span> <span class="n">q</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">item</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">break</span>
        <span class="n">do_work</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
        <span class="n">q</span><span class="o">.</span><span class="n">task_done</span><span class="p">()</span>

<span class="n">q</span> <span class="o">=</span> <span class="n">queue</span><span class="o">.</span><span class="n">Queue</span><span class="p">()</span>
<span class="n">threads</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_worker_threads</span><span class="p">):</span>
    <span class="n">t</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">worker</span><span class="p">)</span>
    <span class="n">t</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
    <span class="n">threads</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>

<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">source</span><span class="p">():</span>
    <span class="n">q</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>

<span class="c1"># block until all tasks are done</span>
<span class="n">q</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>

<span class="c1"># stop workers</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_worker_threads</span><span class="p">):</span>
    <span class="n">q</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
<span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">threads</span><span class="p">:</span>
    <span class="n">t</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="simplequeue-objects">
<h2>SimpleQueue 对象<a class="headerlink" href="#simplequeue-objects" title="永久链接至标题">¶</a></h2>
<p><a class="reference internal" href="#queue.SimpleQueue" title="queue.SimpleQueue"><code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleQueue</span></code></a> 对象提供下列描述的公共方法。</p>
<dl class="method">
<dt id="queue.SimpleQueue.qsize">
<code class="sig-prename descclassname">SimpleQueue.</code><code class="sig-name descname">qsize</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.SimpleQueue.qsize" title="永久链接至目标">¶</a></dt>
<dd><p>返回队列的大致大小。注意，qsize() &gt; 0 不保证后续的 get() 不被阻塞。</p>
</dd></dl>

<dl class="method">
<dt id="queue.SimpleQueue.empty">
<code class="sig-prename descclassname">SimpleQueue.</code><code class="sig-name descname">empty</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.SimpleQueue.empty" title="永久链接至目标">¶</a></dt>
<dd><p>如果队列为空，返回 <code class="docutils literal notranslate"><span class="pre">True</span></code> ，否则返回 <code class="docutils literal notranslate"><span class="pre">False</span></code> 。如果 empty() 返回 <code class="docutils literal notranslate"><span class="pre">False</span></code> ，不保证后续调用的 get() 不被阻塞。</p>
</dd></dl>

<dl class="method">
<dt id="queue.SimpleQueue.put">
<code class="sig-prename descclassname">SimpleQueue.</code><code class="sig-name descname">put</code><span class="sig-paren">(</span><em class="sig-param">item</em>, <em class="sig-param">block=True</em>, <em class="sig-param">timeout=None</em><span class="sig-paren">)</span><a class="headerlink" href="#queue.SimpleQueue.put" title="永久链接至目标">¶</a></dt>
<dd><p>将 <em>item</em> 放入队列。此方法永不阻塞，始终成功（除了潜在的低级错误，例如内存分配失败）。可选参数 <em>block</em> 和 <em>timeout</em> 仅仅是为了保持 <a class="reference internal" href="#queue.Queue.put" title="queue.Queue.put"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Queue.put()</span></code></a> 的兼容性而提供，其值被忽略。</p>
<div class="impl-detail compound">
<p><strong>CPython implementation detail:</strong> This method has a C implementation which is reentrant.  That is, a
<code class="docutils literal notranslate"><span class="pre">put()</span></code> or <code class="docutils literal notranslate"><span class="pre">get()</span></code> call can be interrupted by another <code class="docutils literal notranslate"><span class="pre">put()</span></code>
call in the same thread without deadlocking or corrupting internal
state inside the queue.  This makes it appropriate for use in
destructors such as <code class="docutils literal notranslate"><span class="pre">__del__</span></code> methods or <a class="reference internal" href="weakref.html#module-weakref" title="weakref: Support for weak references and weak dictionaries."><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code></a> callbacks.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="queue.SimpleQueue.put_nowait">
<code class="sig-prename descclassname">SimpleQueue.</code><code class="sig-name descname">put_nowait</code><span class="sig-paren">(</span><em class="sig-param">item</em><span class="sig-paren">)</span><a class="headerlink" href="#queue.SimpleQueue.put_nowait" title="永久链接至目标">¶</a></dt>
<dd><p>相当于 <code class="docutils literal notranslate"><span class="pre">put(item)</span></code> ，仅为保持 <a class="reference internal" href="#queue.Queue.put_nowait" title="queue.Queue.put_nowait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Queue.put_nowait()</span></code></a> 兼容性而提供。</p>
</dd></dl>

<dl class="method">
<dt id="queue.SimpleQueue.get">
<code class="sig-prename descclassname">SimpleQueue.</code><code class="sig-name descname">get</code><span class="sig-paren">(</span><em class="sig-param">block=True</em>, <em class="sig-param">timeout=None</em><span class="sig-paren">)</span><a class="headerlink" href="#queue.SimpleQueue.get" title="永久链接至目标">¶</a></dt>
<dd><p>从队列中移除并返回一个项目。如果可选参数 <em>block</em> 是 true 并且 <em>timeout</em> 是 <code class="docutils literal notranslate"><span class="pre">None</span></code> (默认值)，则在必要时阻塞至项目可得到。如果 <em>timeout</em> 是个正数，将最多阻塞 <em>timeout</em> 秒，如果在这段时间内项目不能得到，将引发 <a class="reference internal" href="#queue.Empty" title="queue.Empty"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Empty</span></code></a> 异常。反之 (<em>block</em> 是 false) , 如果一个项目立即可得到，则返回一个项目，否则引发 <a class="reference internal" href="#queue.Empty" title="queue.Empty"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Empty</span></code></a> 异常 (这种情况下，<em>timeout</em> 将被忽略)。</p>
</dd></dl>

<dl class="method">
<dt id="queue.SimpleQueue.get_nowait">
<code class="sig-prename descclassname">SimpleQueue.</code><code class="sig-name descname">get_nowait</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.SimpleQueue.get_nowait" title="永久链接至目标">¶</a></dt>
<dd><p>相当于 <code class="docutils literal notranslate"><span class="pre">get(False)</span></code> 。</p>
</dd></dl>

<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt>类 <a class="reference internal" href="multiprocessing.html#multiprocessing.Queue" title="multiprocessing.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">multiprocessing.Queue</span></code></a></dt><dd><p>一个用于多进程上下文的队列类（而不是多线程）。</p>
</dd>
</dl>
<p><a class="reference internal" href="collections.html#collections.deque" title="collections.deque"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.deque</span></code></a> 是无界队列的替代实现，具有快速原子的 <a class="reference internal" href="collections.html#collections.deque.append" title="collections.deque.append"><code class="xref py py-meth docutils literal notranslate"><span class="pre">append()</span></code></a> 和 <a class="reference internal" href="collections.html#collections.deque.popleft" title="collections.deque.popleft"><code class="xref py py-meth docutils literal notranslate"><span class="pre">popleft()</span></code></a> 操作，不需要锁定。</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">目录</a></h3>
  <ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code> --- 一个同步的队列类</a><ul>
<li><a class="reference internal" href="#queue-objects">Queue对象</a></li>
<li><a class="reference internal" href="#simplequeue-objects">SimpleQueue 对象</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="sched.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sched</span></code> --- 事件调度器</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="_thread.html"
                        title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">_thread</span></code> --- 底层多线程 API</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/queue.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="_thread.html" title="_thread --- 底层多线程 API"
             >下一页</a> |</li>
        <li class="right" >
          <a href="sched.html" title="sched --- 事件调度器"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="concurrency.html" >并发执行</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>